/**
 * Servlet DBServlet
 * - zabezpeceny pristup pres login.jsp
 * - pri autentizaci dojde k vypsani obsahu databaze
 * - zatim cteni z databaze na localhostu, vypsani nahodnych dat z databze
 * - servlet sposten pod jmenen "test2" ... deklarace pomoci @Webserlvet("/test2") 
 */
package testPackage;
// File: ShowBedrock.java

/* A servlet to display the contents of the MySQL Bedrock database */

import java.io.*;
import java.net.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/test1")
public class DBServlet extends HttpServlet 
{
    public String getServletInfo()
    {
       return "Servlet connects to MySQL database and displays result of a SELECT";
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
            {
    			doGet(request, response);
            }
    // Use http GET
    /**
     * Metoda na obsluhu
     * - pripojeni do DB
     * - nacteni dat a vygenerovani vypisu (tabulky)
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
    	
        String loginUser = "A";
        String loginPasswd = "aaa";
        String loginUrl = "jdbc:mysql://localhost:3306/skola";

        response.setContentType("text/html");    // Nastaveni typu vystupu na html

        
        PrintWriter out = response.getWriter();  // STDOUT

        out.println("<HTML><HEAD><TITLE>Databaze</TITLE></HEAD>");		// Jmeno
        out.println("<BODY><H1>Databaze</H1>");

        
        try
           {
              Class.forName("com.mysql.jdbc.Driver");
              Connection dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
              // Declare our statement
              Statement statement = dbcon.createStatement();

              String query = "SELECT Prvni, Druhy, ";
              query +=       "       Treti ";
              query +=       "FROM   result ";

              // Perform the query
              ResultSet rs = statement.executeQuery(query);

              out.println("<TABLE border>");

              // Iterate through each row of rs
             out.println("<th> Datum</th><th>Cas</th><th>Tep</th><th>Oxidace</th><th>Teplota</th>");
              while (rs.next())
              {
                  String Prvni = rs.getString("Prvni");
                  String Druhy = rs.getString("Druhy");
                  String Treti = rs.getString("Treti");
                  out.println("<tr>" +
                              "<td>" + Prvni + "</td>" +
                              "<td>" + Druhy + "</td>" +
                              "<td>" + Treti + "</td>" +
                              "</tr>");
              }

              out.println("</TABLE>");

              rs.close();
              statement.close();
              dbcon.close();
            }
        catch (SQLException ex) {
              while (ex != null) {
                    System.out.println ("SQL Exception:  " + ex.getMessage ());
                    ex = ex.getNextException ();
                }  // end while
            }  // end catch SQLException

        catch(java.lang.Exception ex)
            {
                out.println("<HTML>" +
                            "<HEAD><TITLE>" +
                            "Databaze: Error" +
                            "</TITLE></HEAD>\n<BODY>" +
                            "<P>SQL error in doGet: " +
                            ex.getMessage() + "</P></BODY></HTML>");
                return;
            }
         out.close();
    }
}